<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class services extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('mModel');
    }

    function index() {
        $this->cekStatus();
        $this->cekExpire();
        $this->load->view('service');
    }

    function cekStatus() {
        $cek = $this->mModel->cekSms();
        foreach ($cek->result() as $listcek) {
            $txt = explode("#", $listcek->TextDecoded);

            switch (strtoupper($txt[0])) {
                case 'STATUS':
                    if ($this->valUserNumber($listcek->SenderNumber, $txt[1])) {
                        $exp = $this->mModel->getExpiration($txt[1]);
                        $exp = $exp->row();

                        $reply['DestinationNumber'] = $listcek->SenderNumber;
                        $reply['TextDecoded'] = 'Masa aktif user Anda sampai dengan ' . $exp->value . ' . Terimakasih. -PenguinNetwork-';
                        $this->mModel->insertSms('outbox', $reply);

                        $repUp['processed'] = 'true';

                        $this->mModel->updateSms('inbox', $listcek->ID, $repUp);
                    }
            }
        }
    }

    function cekExpire() {
        error_reporting(0);
        $cek = $this->mModel->getListExpiration();
        foreach ($cek->result() as $listcek) {
            date_default_timezone_set('Asia/Jakarta');
            $tanggalSekarang = date("F d Y G:i:s");

            $selisih = strtotime($listcek->value) - strtotime($tanggalSekarang);
            $hari = $selisih / (60 * 60 * 12);

            $ckN = $this->mModel->getNotifStatus($listcek->username);
            $ckN = $ckN->row();

            if ($hari < 1 && $ckN->notif == "false") {
                $exp = $this->mModel->getExpiration($listcek->username);
                $exp = $exp->row();

                $ph = $this->mModel->getPhoneNumber($listcek->username);
                $ph = $ph->row();

                $sms['DestinationNumber'] = $ph->telpon;
                $sms['TextDecoded'] = 'Masa aktif akun internet Anda akan segera berakhir pada ' . $exp->value . ' . Terimakasih. -PenguinNetwork-';
                $this->mModel->insertSms('outbox', $sms);

                $repUp['notif'] = 'true';

                $this->mModel->updateRadius('user', $listcek->username, $repUp);
            }
        }
    }

    function valUserNumber($nomer, $user) {
        $cek = $this->mModel->valUserNumber($nomer, $user);
        $cek = $cek->row();
        if ($cek != null)
            return true;
        else
            return false;
    }

}

?>
